package com.zttek.videoShareProject.modules.service.impl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.videoShareProject.common.json.UserJson;
import com.zttek.videoShareProject.modules.model.User;
import com.zttek.videoShareProject.modules.mapper.UserMapper;
import com.zttek.videoShareProject.modules.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CBY
 * @since 2018-07-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IUserService iUserService;
    public User selectUserById(Long userid) throws Exception {
        return userMapper.selectById(userid);
    }

    public User selectUserByAccount(String account) {
        EntityWrapper<User> wrapper=new EntityWrapper<User>();
        wrapper.eq("account",account);
        List<User> userList =userMapper.selectList(wrapper);
        return userList.size()>0?userList.get(0):null;
    }

    public List<User> selectUserList(int pageNum) throws Exception {
        return null;
    }

    public Page<User> getUserList(int pn, int status) {
        EntityWrapper<User> wrapper=new EntityWrapper<User>();
        wrapper.eq("status",1);
        Page<User> userPage=new Page<User>();
        userPage.setCurrent(pn);
        userPage.setSize(8);
        userPage.setAsc(false);
        userPage.setOrderByField("create_date");
        Page<User> page=  iUserService.selectPage(userPage,wrapper);
        return page;
    }

    public List<UserJson> getUserJsonList(Page<User> page) {
        List<User> list=page.getRecords();
        List<UserJson> list1=new ArrayList<UserJson>();
        for (User user:list) {
            UserJson  userJson=new UserJson(user);
            list1.add(userJson);
        }
        return list1;
    }

    public Page<User> findUserByAccount(String name) {
        EntityWrapper<User> wrapper=new EntityWrapper<User>();
        wrapper.eq("status",1).like("account","%"+name+"%");
        Page<User> userPage=new Page<User>();
        userPage.setCurrent(1);
        userPage.setSize(8);
        userPage.setAsc(false);
        userPage.setOrderByField("create_date");
        Page<User> page=  iUserService.selectPage(userPage,wrapper);
        return page;
    }
}
